<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Model\Admin\Cat;
class CatController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
   
        $data1 = DB::table('cats')->get();
        $data2 = Cat::tree($data1);
       return view('admin.cat_list',['cat' => $data2]);
       
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
         $data1 = DB::table('cats')->get();
         $data2 = Cat::tree($data1);
        return view('admin.cat_add',['cat'=>$data2]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
     //自定义认证
      $this->validate($request, [
        'name'      => 'required|max:255',
        ]);
      //插入数据库
       DB::table('cats')->insert([
            'cat_name'     => $request->name,
            'pid'    => $request->pid,
         ]);
       return redirect('/admin/cat_suc');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
        $data = Cat::find($id);
        return view('admin.cat_edit',['cat'=>$data]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //自定义认证
      $this->validate($request, [
        'name'      => 'required|max:255',
        ]);
      //更新数据库
      Cat::where('id',$id)->update(['cat_name'=>$request->name]);
      return redirect('/admin/cat');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function suc()
    {
        return view('admin.cat_suc');
    }
 

}
